﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using System.Collections;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.Display;

namespace Homework5
{
    public partial class Form1 : Form
    {
        public static Form1 form1;
        private ILayer m_Layer;
        private Boolean isStart = false;
        private Boolean isFirstDown;
        public IFeatureClass pFClass;//定义为公共的，让form2可以使用
        short transparency = 0;//透明度 
        public Form1()
        {
            form1 = this;
            InitializeComponent();
        }

        private void axLicenseControl1_Enter(object sender, EventArgs e)
        {

        }

        private void contextMenuStrip1_Opening(object sender, CancelEventArgs e)
        {

        }

        private void axTOCControl1_OnMouseDown(object sender, ITOCControlEvents_OnMouseDownEvent e)
        {
            if (e.button == 2)
            {
                ESRI.ArcGIS.Controls.esriTOCControlItem Item = ESRI.ArcGIS.Controls.esriTOCControlItem.esriTOCControlItemNone;
                IBasicMap pBasicMap = null;
                ILayer pLayer = null;
                object other = null;
                object index = null;
                axTOCControl1.HitTest(e.x, e.y, ref Item, ref pBasicMap, ref pLayer, ref other, ref index);          //实现赋值
                m_Layer = pLayer;
                IFieldInfoSet pFields = pLayer as FieldInfoSetClass;
                if (Item == esriTOCControlItem.esriTOCControlItemLayer)           //点击的是图层的话，就显示右键菜单
                {
                    contextMenuStrip1.Show(axTOCControl1, new System.Drawing.Point(e.x, e.y));
                    //显示右键菜单，并定义其相对控件的位置，正好在鼠标出显示
                }
                //else if (Item == esriTOCControlItem.esriTOCControlItemLegendClass)
                //{
                //    contextMenuStrip2.Show(axTOCControl1, new System.Drawing.Point(e.x, e.y));
                //}
            }
        }

        private void Form1_Resize(object sender, EventArgs e)
        {
            //axTOCControl1.Width = this.Width / 5 * 2;
            ////axTOCControl1.Height = this.Height / 3 * 2;
            //axToolbarControl1.Width = this.Width / 5 * 3;
            ////axToolbarControl1.Height = this.Height;
            //groupBox1.Width = this.Width / 5 * 2;
            ////groupBox1.Height = this.Height / 3;
            //tabControl1.Width = this.Width / 5 * 2;
            ////tabControl1.Height = this.Height;
        }

        private void 删除图层ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < axMapControl1.LayerCount; i++)
            {
                 if (axMapControl1.get_Layer(i) == m_Layer)//通过for循环得到选中图层索引，并直接调用DeleteLayer方法定点删除
                 axMapControl1.DeleteLayer(i);
            }
            axMapControl1.ActiveView.Refresh();
        }

        private void 文件ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IMap pMap = axMapControl1.Map;
            int toIndex = 0;
            for (int i = 0; i < axMapControl1.LayerCount; i++)
            {
                
                if (axMapControl1.get_Layer(i) == m_Layer)//通过for循环得到选中图层索引
                    toIndex = i;
            }
            if (toIndex > 1)
            {
                toIndex--;//上移减一
                pMap.MoveLayer(m_Layer, toIndex);
                axMapControl1.ActiveView.Refresh();
                axTOCControl1.Update();
            }
        }

        private void 下移图层ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IMap pMap = axMapControl1.Map;
            int toIndex = 0;
            for (int i = 0; i < axMapControl1.LayerCount; i++)
            {

                if (axMapControl1.get_Layer(i) == m_Layer)//通过for循环得到选中图层索引
                    toIndex = i;
            }
            if (toIndex < axMapControl1.LayerCount)
            {
                toIndex++;//下移加一
                pMap.MoveLayer(m_Layer, toIndex);
                axMapControl1.ActiveView.Refresh();
                axTOCControl1.Update();
            }
        }

        
        private void Form1_Load(object sender, EventArgs e)
        {
            axToolbarControl1.AddMenuItem("esriControls.ControlsMapViewMenu", -1, false, -1);

        }

        
/*
 *
 * 实验四代码部分
 * 
 */
        /*
         * 地图取点
         */
        private void button1_Click(object sender, EventArgs e)
        {
            radioButton1.Enabled = true;
            radioButton2.Enabled = true;
            radioButton3.Enabled = true;
            isStart = true;
        }
        //当点击了start按钮后，开始记录点击的点坐标
        private void axMapControl1_OnMouseDown(object sender, IMapControlEvents2_OnMouseDownEvent e)
        {
            //必须是点击了start按钮才能进行画点、线、面
            if (isStart)
            {
                IMap pMap;
                IActiveView pActiveView;
                pMap = axMapControl1.Map;
                pActiveView = pMap as IActiveView;
                IGraphicsContainer pGraphicsContainer = pMap as IGraphicsContainer;
                //画点
                if (radioButton1.Checked)
                {

                    IPoint pPoint = axMapControl1.ToMapPoint(e.x, e.y);
                    IMarkerElement pMarkerElement = new MarkerElementClass();
                    IElement pElement = pMarkerElement as IElement;
                    pElement.Geometry = pPoint;
                    pGraphicsContainer.AddElement((IElement)pMarkerElement, 0);
                    pActiveView.Refresh();
                }
                //画多义线
                //双击后停止画多义线
                if (radioButton2.Checked && !isFirstDown)
                {
                    IGeometry polyline;
                    polyline = axMapControl1.TrackLine();
                    ILineElement pLineElement;
                    pLineElement = new LineElementClass();
                    IElement pElement;
                    pElement = pLineElement as IElement;
                    pElement.Geometry = polyline;

                    pGraphicsContainer.AddElement((IElement)pLineElement, 0);
                    pActiveView.Refresh();
                }

                if (radioButton3.Checked && !isFirstDown)
                {
                    IGeometry Polygon = axMapControl1.TrackPolygon();
                    IPolygonElement PolygonElement = new PolygonElementClass();
                    IElement pElement = PolygonElement as IElement;
                    pElement.Geometry = Polygon;
                    pGraphicsContainer = pMap as IGraphicsContainer;
                    pGraphicsContainer.AddElement((IElement)PolygonElement, 0);
                    pActiveView.Refresh();

                }
                isFirstDown = false;
            }
        }
        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            isFirstDown = true;
        }
        private void radioButton3_CheckedChanged(object sender, EventArgs e)
        {
            isFirstDown = true;
        }
        //点击stop按钮后
        private void button2_Click(object sender, EventArgs e)
        {
            radioButton1.Enabled = false;
            radioButton2.Enabled = false;
            radioButton3.Enabled = false;
            isStart = false;
        }
        /*
         * 缩放地图
         */
        private void button3_Click(object sender, EventArgs e)
        {
            radioButton4.Enabled = true;
            radioButton5.Enabled = true;
        }
        private void radioButton4_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton4.Checked)
            {
                ICommand pCommand = new ControlsMapZoomInToolClass();
                pCommand.OnCreate(axMapControl1.Object);
                axMapControl1.CurrentTool = pCommand as ITool;

            }
        }
        private void radioButton5_CheckedChanged(object sender, EventArgs e)
        {
            if (radioButton5.Checked)
            {
                ICommand pCommand = new ControlsMapZoomOutToolClass();
                pCommand.OnCreate(axMapControl1.Object);
                axMapControl1.CurrentTool = pCommand as ITool;

            }
        }

        //结束
        private void button4_Click(object sender, EventArgs e)
        {
            radioButton4.Enabled = false;
            radioButton5.Enabled = false;
        }

        public void 修改图层ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IFeatureLayer pFLayer = m_Layer as IFeatureLayer;
            IFeatureClass pFClass = pFLayer.FeatureClass;
            
            Form2 form2 = new Form2(pFClass);
            form2.Show();
        }

        private void axMapControl1_OnMouseUp(object sender, IMapControlEvents2_OnMouseUpEvent e)
        {
            IFeatureLayer pFeatureLayer = axMapControl1.get_Layer(0) as IFeatureLayer;
            IPoint point = new PointClass();
            point.PutCoords(e.mapX, e.mapY);

            ISpatialFilter spatialFilter = new SpatialFilter();
            spatialFilter.Geometry = point;
            spatialFilter.SpatialRel = esriSpatialRelEnum.esriSpatialRelIntersects;
            IFeatureCursor featureCursor = pFeatureLayer.Search(spatialFilter, false);

            IFeature pFeature;
            while((pFeature=featureCursor.NextFeature())!=null)
            {
                axMapControl1.FlashShape(pFeature.Shape);
                String text = "";
                for (int i = 0; i < pFeature.Fields.FieldCount; i++)
                {
                    text += pFeature.Fields.Field[i].Name + ":" + pFeature.get_Value(i) + "\n";
                }
                     
                MessageBox.Show(text);
            }
        }

        private void 查询人口数量大于2500的区域ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IFeatureLayer pFLayer = axMapControl1.get_Layer(0) as IFeatureLayer;
            IFeatureClass pFClass = pFLayer.FeatureClass;
            IFields pFields = pFClass.Fields;
            IFeatureCursor pFeaCursor = pFClass.Search(null, true);
            IFeature pFeature = pFeaCursor.NextFeature();
            while (pFeature != null)
            {
                if ((int)pFeature.get_Value(pFeature.Fields.FindField("pop")) > 2500)
                {
                    axMapControl1.FlashShape(pFeature.Shape);
                }
                pFeature = pFeaCursor.NextFeature();
            }
        }

        private void 查询面积大于25000000平方米的区域ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IFeatureLayer pFLayer = axMapControl1.get_Layer(0) as IFeatureLayer;
            IFeatureClass pFClass = pFLayer.FeatureClass;
            IFields pFields = pFClass.Fields;
            IFeatureCursor pFeaCursor = pFClass.Search(null, true);
            IFeature pFeature = pFeaCursor.NextFeature();
            while (pFeature != null)
            {
                if ((int)pFeature.get_Value(pFeature.Fields.FindField("area")) > 25000000)
                {
                    axMapControl1.FlashShape(pFeature.Shape);
                }
                pFeature = pFeaCursor.NextFeature();
            }
        }

        private void 增加透明度ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            transparency += 10;
            ILayer pLayer = this.axMapControl1.get_Layer(0);　
            ILayerEffects pLayerEffects = pLayer as ILayerEffects;
            short sp = transparency;
            pLayerEffects.Transparency = transparency;
            axMapControl1.Refresh();
                
        }

        private void 减小透明度ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            transparency -= 10;
            ILayer pLayer = this.axMapControl1.get_Layer(0);　
            ILayerEffects pLayerEffects = pLayer as ILayerEffects;
            short sp = pLayerEffects.Transparency;
            pLayerEffects.Transparency = transparency;
            axMapControl1.Refresh();
        }
/*
 * 
 *实验五代码部分 
 * 
 */
        private void 唯一值符号化ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IMap pMap = axMapControl1.Map;
            Form3 form3 = new Form3(pMap);
            form3.Show();
        }

        private void 分级色彩符号化ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IMap pMap = axMapControl1.Map;
            Form4 form4 = new Form4(pMap);
            form4.Show();
        }

        public void 唯一值符号化(string field,int layer_index)
        {
            IGeoFeatureLayer m_pGeoFeatureL;
            IUniqueValueRenderer pUniqueValueR;
            IFillSymbol pFillSymbol;
            IColor pNextUniqueColor;
            IEnumColors pEnumRamp;
            ITable pTable;
            int lfieldNumber;
            IRow pNextRow;
            IRowBuffer pNextRowBuffer;
            ICursor pCursor;
            IQueryFilter pQueryFilter;
            string codeValue;
            IRandomColorRamp pColorRamp;

            IMap pMap = axMapControl1.Map;
            pMap.ReferenceScale = 0;
            m_pGeoFeatureL = (IGeoFeatureLayer)pMap.get_Layer(layer_index);
            pUniqueValueR = new UniqueValueRendererClass();
            pTable = (ITable)m_pGeoFeatureL;

            lfieldNumber = pTable.FindField(field);
            if (lfieldNumber == -1)
            {
                MessageBox.Show("未能找到字段 " + field);
                return;
            }
            //只用一个字段进行单值着色
            pUniqueValueR.FieldCount = 1;
            //用于区分着色的字段
            pUniqueValueR.set_Field(0, field);
            //产生一个随机的颜色条，用的是HSV颜色模式
            pColorRamp = new RandomColorRampClass();
            pColorRamp.StartHue = 0;
            pColorRamp.MinValue = 99;
            pColorRamp.MinSaturation = 15;
            pColorRamp.EndHue = 360;
            pColorRamp.MaxValue = 100;
            pColorRamp.MaxSaturation = 30;
            //任意产生100个颜色，如果知道要素的数目可以产生精确的颜色个数
            pColorRamp.Size = 100;
            bool ok = true;
            pColorRamp.CreateRamp(out ok);
            pEnumRamp = pColorRamp.Colors;
            pNextUniqueColor = null;
            //产生查询过滤器对象
            pQueryFilter = new QueryFilterClass();
            pQueryFilter.AddField(field);
            //根据某个字段在表中找出指向所有行的游标对象
            pCursor = pTable.Search(pQueryFilter, true);
            pNextRow = pCursor.NextRow();
            //遍历所有的要素
            while (pNextRow != null)
            {
                pNextRowBuffer = pNextRow;
                //找出Row的值
                codeValue = pNextRowBuffer.get_Value(lfieldNumber).ToString();
                //获取随机颜色带中的任意一个颜色
                pNextUniqueColor = pEnumRamp.Next();
                if (pNextUniqueColor == null)
                {
                    pEnumRamp.Reset();
                    pNextUniqueColor = pEnumRamp.Next();
                }
                pFillSymbol = new SimpleFillSymbolClass();
                pFillSymbol.Color = pNextUniqueColor;
                //将每次都得的要素字段值和修饰它的符号值放入着色对象中
                pUniqueValueR.AddValue(codeValue, field, (ISymbol)
               pFillSymbol);
                pNextRow = pCursor.NextRow();
            }
            m_pGeoFeatureL.Renderer = (IFeatureRenderer)pUniqueValueR;
            axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);

        }
        //重载函数
        public void 唯一值符号化(string field)
        {
            IGeoFeatureLayer m_pGeoFeatureL;
            IUniqueValueRenderer pUniqueValueR;
            IFillSymbol pFillSymbol;
            IColor pNextUniqueColor;
            IEnumColors pEnumRamp;
            ITable pTable;
            int lfieldNumber;
            IRow pNextRow;
            IRowBuffer pNextRowBuffer;
            ICursor pCursor;
            IQueryFilter pQueryFilter;
            string codeValue;
            IRandomColorRamp pColorRamp;

            IMap pMap = axMapControl1.Map;
            pMap.ReferenceScale = 0;
            for (int layer_index = 0; layer_index < pMap.LayerCount; layer_index++)
            {
                m_pGeoFeatureL = (IGeoFeatureLayer)pMap.get_Layer(layer_index);
                pUniqueValueR = new UniqueValueRendererClass();
                pTable = (ITable)m_pGeoFeatureL;

                lfieldNumber = pTable.FindField(field);
                if (lfieldNumber == -1)
                {
                    MessageBox.Show("未能找到字段 " + field);
                    return;
                }
                //只用一个字段进行单值着色
                pUniqueValueR.FieldCount = 1;
                //用于区分着色的字段
                pUniqueValueR.set_Field(0, field);
                //产生一个随机的颜色条，用的是HSV颜色模式
                pColorRamp = new RandomColorRampClass();
                pColorRamp.StartHue = 0;
                pColorRamp.MinValue = 99;
                pColorRamp.MinSaturation = 15;
                pColorRamp.EndHue = 360;
                pColorRamp.MaxValue = 100;
                pColorRamp.MaxSaturation = 30;
                //任意产生100个颜色，如果知道要素的数目可以产生精确的颜色个数
                pColorRamp.Size = 100;
                bool ok = true;
                pColorRamp.CreateRamp(out ok);
                pEnumRamp = pColorRamp.Colors;
                pNextUniqueColor = null;
                //产生查询过滤器对象
                pQueryFilter = new QueryFilterClass();
                pQueryFilter.AddField(field);
                //根据某个字段在表中找出指向所有行的游标对象
                pCursor = pTable.Search(pQueryFilter, true);
                pNextRow = pCursor.NextRow();
                //遍历所有的要素
                while (pNextRow != null)
                {
                    pNextRowBuffer = pNextRow;
                    //找出Row的值
                    codeValue = pNextRowBuffer.get_Value(lfieldNumber).ToString();
                    //获取随机颜色带中的任意一个颜色
                    pNextUniqueColor = pEnumRamp.Next();
                    if (pNextUniqueColor == null)
                    {
                        pEnumRamp.Reset();
                        pNextUniqueColor = pEnumRamp.Next();
                    }
                    pFillSymbol = new SimpleFillSymbolClass();
                    pFillSymbol.Color = pNextUniqueColor;
                    //将每次都得的要素字段值和修饰它的符号值放入着色对象中
                    pUniqueValueR.AddValue(codeValue, field, (ISymbol)
                   pFillSymbol);
                    pNextRow = pCursor.NextRow();
                }
                m_pGeoFeatureL.Renderer = (IFeatureRenderer)pUniqueValueR;
                axMapControl1.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
            }
        }

        //界面更新函数
        public void Form1Update()
        {
            axMapControl1.Refresh();

            axTOCControl1.Update();
        }

        
    }
}
